/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.eventtype;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * EventTypeManagerBean
 * ----------------
 * This is the remote interface implementation of Event Type
 *
 *
 * @author Dany Gunawan
 * @lastmodify 2009-05-12
 */
@Stateless
public class EventTypeManagerBean implements EventTypeManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addEventType(EventTypeDetailedTO to) throws EventTypeManagerException {
        try {
            EventTypeEntity et = new EventTypeEntity();
            et.setData(to);
            em.persist(et);
            
        } catch (Exception e) {
            throw new EventTypeManagerException("Cannot add Event Type", e);
        }
    }

    public void editEventType(EventTypeDetailedTO to) throws EventTypeManagerException {
        try {
            EventTypeEntity et = em.find(EventTypeEntity.class, to.getId());
            et.setData(to);
            em.persist(et);
        } catch (Exception e) {
            throw new EventTypeManagerException("Cannot edit Event Type", e);
        }
    }

    public void deleteEventType(Long id) throws EventTypeManagerException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<EventTypeTO> getAllEventTypes() throws EventTypeManagerException {

        List<EventTypeTO> results = null;

        try {
            results = new ArrayList<EventTypeTO>();
            Query allEventTypesQuery = em.createNamedQuery("findAllEventTypes");
            List dbResults = allEventTypesQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                EventTypeEntity et = (EventTypeEntity) dbResults.get(i);
                results.add(et.getData());
            }
        } catch (Exception e) {
            throw new EventTypeManagerException("Cannot get all Event Types", e);
        }

        return results;
    }

    public List<EventTypeDetailedTO> getAllDetailedBookingTypes() throws EventTypeManagerException {
        List<EventTypeDetailedTO> results = null;

        try {
            results = new ArrayList<EventTypeDetailedTO>();
            Query allEventTypesQuery = em.createNamedQuery("findAllEventTypes");
            List dbResults = allEventTypesQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                EventTypeEntity et = (EventTypeEntity) dbResults.get(i);
                results.add(et.getDetailedData());
            }
        } catch (Exception e) {
            throw new EventTypeManagerException("Cannot get all EventTypeDetailedTOs", e);
        }

        return results;

    }

    public EventTypeDetailedTO getEventType(Long id) throws EventTypeManagerException {

        EventTypeEntity et = null;
        EventTypeDetailedTO to = null;
        try {
            et = em.find(EventTypeEntity.class, id);
            to = et.getDetailedData();
        } catch (Exception e) {
            throw new EventTypeManagerException("Cannot get Event Type by id " + id.longValue(), e);
        }
        return to;
    }
}
